from ..config import Base
from .BaseDb import BaseDb
from sqlalchemy import create_engine, Column, Integer, String, Date, DECIMAL, BIGINT, UniqueConstraint, Index
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# 日线
class StockDaily(Base):
    __tablename__ = "stock_daily"

    id = Column(BIGINT, primary_key=True, autoincrement=True)
    ts_code = Column(String(20), nullable=False, comment="股票代码，如 600519.SH")
    trade_date = Column(Date, nullable=False, comment="交易日期")
    open = Column(DECIMAL(10, 2), comment="开盘价")
    high = Column(DECIMAL(10, 2), comment="最高价")
    low = Column(DECIMAL(10, 2), comment="最低价")
    close = Column(DECIMAL(10, 2), comment="收盘价")
    pre_close = Column(DECIMAL(10, 2), comment="昨收价")
    change = Column(DECIMAL(10, 2), comment="涨跌额")
    pct_chg = Column(DECIMAL(6, 2), comment="涨跌幅")
    vol = Column(BIGINT, comment="成交量")
    amount = Column(DECIMAL(20, 2), comment="成交额")

    # 约束 & 索引
    __table_args__ = (
        UniqueConstraint("ts_code", "trade_date", name="uk_stock_date"),  # 唯一索引
        Index("idx_stock_date", "ts_code", "trade_date"),                 # 普通复合索引
        Index("idx_date", "trade_date"),                                 # 日期索引
    )
